package com.accounting.juanjuan.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.accounting.juanjuan.entity.AmountData;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 利用hutool的excel工具类，简单封装一个工具类
 *
 * @Author xxj
 * @Date 2024/1/26 14:27
 * @Version 1.0
 */
public class MyExcelUtil {
    public static ExcelReader getExcelReader(InputStream inputStream, String sheetName) {
        if (StringUtil.isBlank(sheetName)) {
            sheetName = "Sheet1";
        }
        return ExcelUtil.getReader(inputStream, sheetName);
    }

    /**
     * 获取标题（第一行数据）
     */
    public static List<String> getTitle(ExcelReader excelReader) {
        return getRowData(excelReader, 0).stream().map(Object::toString).collect(Collectors.toList());
    }

    /**
     * 获取标题（第一行数据）
     */
    public static List<Object> getTitleAddRowName(ExcelReader excelReader) {
        List<Object> title = getRowData(excelReader, 0);
        title.add(0, "源文件行号");
        return title;
    }

    /**
     * 获取某一行的数据
     */
    public static List<Object> getRowData(ExcelReader excelReader, int row) {
        return excelReader.readRow(row);
    }

    /**
     * 获取某一行的数据，附带源文件行号
     */
    public static List<Object> getRowDataAddRow(ExcelReader excelReader, int row) {
        //从0行开始取
        List<Object> objects = getRowData(excelReader, row - 1);
        // 传来的序号本身就是用户看到的行号
        objects.add(0, row);
        return objects;
    }

    /**
     * 获取全部数据
     */
    public static List<Map<String, Object>> getAllData(ExcelReader excelReader) {
        return excelReader.readAll();
    }

    /**
     * 获取金额实体类数据,返回正负两个
     */
    public static List<List<AmountData>> getPositiveAndNegativeAmountDataList(ExcelReader excelReader, String titleName) {
        List<AmountData> positiveNumbers = new ArrayList<>();
        List<AmountData> negativeNumbers = new ArrayList<>();
        List<Map<String, Object>> allData = getAllData(excelReader);
        for (int i = 0; i < allData.size(); i++) {
            Object o = allData.get(i).get(titleName);
            if (o == null || StringUtil.isBlank(o.toString())) {
                continue;
            }
            double v = Double.parseDouble(o.toString());
            if (v > 0) {
                positiveNumbers.add(new AmountData(i + 2, v));
                continue;
            }
            if (v < 0) {
                negativeNumbers.add(new AmountData(i + 2, v));
            }
        }
        List<List<AmountData>> result = new ArrayList<>();
        result.add(positiveNumbers);
        result.add(negativeNumbers);
        return result;
    }

    /**
     * 写出数据，哪里获取流，哪里关闭流
     */
    public static void writeData(OutputStream outputStream, List<List<Object>> data) {
        ExcelWriter writer = ExcelUtil.getWriter();
        autoSetWith(writer, data);
        writer.write(data);
        writer.flush(outputStream);
        writer.close();
    }

    /**
     * 自动设置数据的宽度
     */
    public static void autoSetWith(ExcelWriter writer, List<List<Object>> data) {
        // 按照 10个宽度， 10个字母 ， 5个汉字来计算,(这样正好够，但是有些拥挤，可以加一些值，就加5吧)
        int initWidth = 5;
        int[] widthArr = new int[data.get(0).size()];
        for (int i = 0; i < data.get(0).size(); i++) {
            widthArr[i] = 0;
        }
        for (List<Object> objects : data) {
            for (int i = 0; i < objects.size(); i++) {
                int width = StringUtil.calculateLength(String.valueOf(objects.get(i)));
                if (width > widthArr[i]) {
                    widthArr[i] = width;
                }
            }
        }
        for (int i = 0; i < widthArr.length; i++) {
            writer.setColumnWidth(i, widthArr[i] + initWidth);
        }
    }
}
